package org.wu.framework.lazy.orm.database.sql.expand.database.persistence.method.ddl;

import lombok.extern.slf4j.Slf4j;
import org.wu.framework.lazy.orm.core.config.enums.DDLAuto;
import org.wu.framework.lazy.orm.core.persistence.conf.LazyTableEndpoint;
import org.wu.framework.lazy.orm.core.source.SqlSourceClass;
import org.wu.framework.lazy.orm.database.lambda.domain.PersistenceRepository;
import org.wu.framework.lazy.orm.database.lambda.stream.LambdaTableType;
import org.wu.framework.lazy.orm.database.sql.expand.database.persistence.method.LazyOperationParameter;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * describe : 创建表结构
 *
 * @author : Jia wei Wu
 * @version 1.0
 * @date : 2022/1/2 7:56 下午
 */
@Slf4j
public class LazyOperationMethodCreateTableEndpoint extends AbstractLazyDDLOperationMethod {
    public LazyOperationMethodCreateTableEndpoint(LazyOperationParameter lazyOperationParameter) {
        super(lazyOperationParameter);
    }

    /**
     * @param sourceParams
     * @return description 通过参数获取持久性存储库对象
     * @author Jia wei Wu
     * @date 2021/4/17 3:38 下午
     **/
    @Override
    public PersistenceRepository doAnalyzePersistenceRepository(Object[] sourceParams) throws Exception {
        PersistenceRepository persistenceRepository = createPersistenceRepository();
        persistenceRepository.setExecutionType(LambdaTableType.DDL);
        List<LazyTableEndpoint> lazyTableEndpointList = new ArrayList<>();
        for (Object sourceParam : sourceParams) {
            if (sourceParam instanceof Object[]) {
                Object[] objects = (Object[]) sourceParam;
                for (Object o : objects) {
                    LazyTableEndpoint lazyTableEndpoint = (LazyTableEndpoint) o;
                    lazyTableEndpointList.add(lazyTableEndpoint);
                }
            } else {
                LazyTableEndpoint lazyTableEndpoint = (LazyTableEndpoint) sourceParam;
                lazyTableEndpointList.add(lazyTableEndpoint);
            }
        }

        String tables = lazyTableEndpointList.stream().map(LazyTableEndpoint::getTableName).collect(Collectors.joining("、"));
        persistenceRepository.setQueryString("执行创建表[" + tables + "]结构sql");
        return persistenceRepository;
    }

    /**
     * describe  执行SQL 语句
     *
     * @param connection
     * @param sourceParams
     * @return
     * @author Jia wei Wu
     * @date 2022/1/2 8:10 下午
     **/
    @Override
    public Object doExecute(Connection connection, Object[] sourceParams) throws Exception {
        // 创建表
        final Object param = sourceParams[0];
        if (param instanceof Object[] objects) {
            for (Object o : objects) {
                LazyTableEndpoint lazyTableEndpoint = (LazyTableEndpoint) o;
                create(lazyTableEndpoint, connection);
            }
        } else {
            LazyTableEndpoint lazyTableEndpoint = (LazyTableEndpoint) param;
            create(lazyTableEndpoint, connection);
        }
        return true;
    }

    protected void create(LazyTableEndpoint lazyTableEndpoint, Connection connection) throws SQLException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        perfect(connection, lazyTableEndpoint, DDLAuto.UPDATE);
        log.warn("create table {} success", lazyTableEndpoint.getTableName());
    }
}
